
//Copyright 1997-2009 Syrinx Development, Inc.
//This file is part of the Syrinx Web Application Framework (SWAF).
// == BEGIN LICENSE ==
//
// Licensed under the terms of any of the following licenses at your
// choice:
//
//  - GNU General Public License Version 3 or later (the "GPL")
//    http://www.gnu.org/licenses/gpl.html
//
//  - GNU Lesser General Public License Version 3 or later (the "LGPL")
//    http://www.gnu.org/licenses/lgpl.html
//
//  - Mozilla Public License Version 1.1 or later (the "MPL")
//    http://www.mozilla.org/MPL/MPL-1.1.html
//
// == END LICENSE ==
using System;
using System.Collections;
using System.Runtime.Remoting;

using Swaf;

namespace Swaf.BizRule.ThreadedQueue
{
	/// <summary>
	/// Summary description for ResultsRuleGroup.
	/// </summary>
	public class ResultsRuleGroup : MarshalByRefObject, IRuleGroup, IRuleGroupControl
	{
		protected BasicRuleGroup m_innerGroup;
		protected BasicResults m_results = new BasicResults();

		public ResultsRuleGroup(IRuleGroupControl parentGroup)
		{
			if(parentGroup == null)
				parentGroup = this;
			m_innerGroup = new BasicRuleGroup(parentGroup);
		}

		internal void addResult(SingleCallAndResult r)
		{
			m_results.add(r);
		}
		#region IRuleGroup Members

		public virtual IInitialRequest MainRequest{get{return m_innerGroup.MainRequest;}}
		public virtual IRuleGroup createChildGroup(bool maintainResults)
		{
			return m_innerGroup.createChildGroup(maintainResults);
			/*
			IRuleGroup childGroup;
			if(maintainResults)
				childGroup = new ResultsRuleGroup(this);
			else
				childGroup = new BasicRuleGroup(this);
			m_innerGroup.addChildRuleGroup(childGroup);
			return childGroup;
			*/
		}

		public void waitForCompletion(bool deepWait)
		{
			m_innerGroup.Group.waitForCompletion(deepWait);
		}

		public void continueExecution()
		{
			m_innerGroup.Group.continueExecution();
		}

		public void pauseExecution()
		{
			m_innerGroup.Group.pauseExecution();
		}


		public void cancelExecution()
		{
			m_innerGroup.Group.cancelExecution();
		}

		public int RulesCompletedCount	{get{return m_innerGroup.Group.RulesCompletedCount;}}

		public int TotalRules {get{return m_innerGroup.Group.TotalRules;}}
		#endregion

		#region IResults Members
		public int numberOfResults {get{return m_results.numberOfResults;}}
		public object CallResults {get{return m_results.CallResults;}}
		public object getCallResults(int index) {return m_results.getCallResults(index);}
		public Exception ExceptionInfo {get{return m_results.ExceptionInfo;}}
		public string getXml() {return m_results.getXml();}
		public Exception getExceptionInfo(int index) {return m_results.getExceptionInfo(index);}
		#endregion

		#region IRuleExecutor Members

		public IRuleGroup[] execute(string[] ruleName, object[] callParams, object callback, string callbackMethod)
		{
			return m_innerGroup.Group.execute(ruleName, callParams, callback, callbackMethod);
		}

		public IResults execute(string ruleCallInfo)
		{
			return m_innerGroup.Group.execute(ruleCallInfo);
		}

		public IResults execute(string ruleName, ICollection callParams, bool multiCall)
		{
			return m_innerGroup.Group.execute(ruleName, callParams, multiCall);
		}

		public IResults execute(IRuleContext context, string[] ruleNames, object[] callParams)
		{
			return m_innerGroup.Group.execute(context, ruleNames, callParams);
		}

		public IResults execute(string[] ruleNames, object[] callParams)
		{
			return m_innerGroup.Group.execute(ruleNames, callParams);
		}

		public IRuleGroup execute(string ruleCallInfo, object callback, string callbackMethod)
		{
			return m_innerGroup.Group.execute(ruleCallInfo, callback, callbackMethod);
		}

		public IRuleGroup execute(IRuleContext context, string ruleName, object callback, string callbackMethod, object callParams)
		{
			SingleCallAndResult r = m_innerGroup.ownerWantsRuleInfo(ruleName, callParams);
			m_results.add(r);
			return m_innerGroup.Group.execute(context, ruleName, callback, callbackMethod, r);
		}

		public IRuleGroup execute(string ruleName, object callback, string callbackMethod, object callParams)
		{
			SingleCallAndResult r = m_innerGroup.ownerWantsRuleInfo(ruleName, callParams);
			m_results.add(r);
			return m_innerGroup.Group.execute(ruleName, callback, callbackMethod, r);

			//return m_innerGroup.Group.execute(ruleName, callback, callbackMethod, callParams);
		}

		public IResults execute(IRuleContext context, string ruleName, params object[] callParams)
		{
			return m_innerGroup.Group.execute(context, ruleName, callParams);
		}

		public IResults execute(string ruleName, params object[] callParams)
		{
			return m_innerGroup.Group.execute(ruleName, callParams);
		}
		#endregion

		#region IRuleGroupControl Members

		public void calcWaitGroup(IRuleGroup child)
		{
			if(m_innerGroup != child)
				m_innerGroup.calcWaitGroup(child);
		}

		public void ruleCompleted()
		{
			m_innerGroup.ruleCompleted();
		}

		public IRuleGroup Group
		{
			get
			{
				return this;
			}
		}

		public IRuleGroup AnonymousGroup()
		{
			return m_innerGroup;
		}

		public int getNotDoneRuleCount(bool deep)
		{
			return m_innerGroup.getNotDoneRuleCount(deep);
		}

		/*
		public IList ChildRuleGroups
		{
			get
			{
				return m_innerGroup.ChildRuleGroups;
			}
		}
*/
		public void ruleAdded()
		{
			m_innerGroup.ruleAdded();
		}
		#endregion
	}
}
